package com.example.plantuml.demo.service;

import com.example.plantuml.demo.model.User;
import org.springframework.stereotype.Service;

@Service
public class ValidationService {

    public void validateId(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("无效的ID: " + id);
        }
        
        // 模拟验证处理时间
        simulateDelay(30);
    }
    
    public void validateUser(User user) {
        if (user == null) {
            throw new IllegalArgumentException("用户信息不能为空");
        }
        
        if (user.getName() == null || user.getName().trim().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        
        if (user.getEmail() == null || !user.getEmail().contains("@")) {
            throw new IllegalArgumentException("无效的邮箱地址");
        }
        
        if (user.getAge() < 0 || user.getAge() > 150) {
            throw new IllegalArgumentException("无效的年龄: " + user.getAge());
        }
        
        // 模拟验证处理时间
        simulateDelay(80);
    }
    
    // 模拟处理延迟
    private void simulateDelay(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
